//
//  LSZPoetryLyricBooksVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/19.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZPoetryLyricBooksVC.h"
#import "LCFloatingWindow.h"
#import "LSZPoetryLyricBooksVC+LCAVPlay.h"
#import "JKSongsMessStatus.h"
#import "NetworkTool+LCAVPlay.h"

@interface LSZPoetryLyricBooksVC ()

@end

@implementation LSZPoetryLyricBooksVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.numberPage = 1;
 
    [self dataReuestDic:self.numberPage];
    
    [LCFloatingWindow sharedInstance].hidden = NO;

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [LCFloatingWindow sharedInstance].hidden = YES;
    if(self.viewSharePopover.hidden == NO){
        self.viewSharePopover.hidden = YES;
        self.viewLow.hidden = YES;
        self.butCancel.hidden = YES;
    }
}

#pragma mark - 数据的请求
-(void)dataReuestDic :(NSInteger)numberPage
{
    //获得文件路径
    // 1.获得沙盒根路径
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path1 = [pathArray objectAtIndex:0];
    NSString *myPath = [path1 stringByAppendingPathComponent:@"LSZlyricBooks.plist"];
    NSDictionary * diction = [[NSDictionary alloc]initWithContentsOfFile:myPath];
    __weak typeof(self) weakSelf = self;
    if (diction.allKeys.count >0) {
        LSZPoetryManager * PoetryMg = [[LSZPoetryManager alloc]initWithDicPoetryManager:diction];
        [weakSelf.mutArrayData removeAllObjects];
        [weakSelf.mutArrayData setArray:PoetryMg.mutDataArray];
        [weakSelf.tableviewPoetry reloadData];
    }else{
        [LSZRequestResolutionDic initWithPoetry:[NSString stringWithFormat:@"%ld",(long)numberPage] :^(BOOL isOK, NSString * Str, LSZPoetryManager *PoetryManager) {
            if (isOK == YES) {
                if (numberPage == 1) {
                    [weakSelf.mutArrayData removeAllObjects];
                    [weakSelf.mutArrayData setArray:PoetryManager.mutDataArray];
                }else{
                    [weakSelf.mutArrayData addObjectsFromArray:PoetryManager.mutDataArray];
                }
                [weakSelf.tableviewPoetry reloadData];
            }else{
                [self addHubPromptMessage:Str];
            }
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"诗 歌";
    [self creareAVPlay];//取title的值进行判断 ///////////

    self.mutArrayData = [[NSMutableArray alloc]init];
    //诗歌歌谱的上下拉
    self.tableviewPoetry.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.numberPage = 1;
        //诗歌歌谱的分类
        [self dataReuestDic:self.numberPage];
        [self.tableviewPoetry.mj_header endRefreshing];
    }];
    // 上拉刷新
    self.tableviewPoetry.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.numberPage++;
        //诗歌歌谱的分类
        [self dataReuestDic:self.numberPage];
        [self.tableviewPoetry.mj_footer endRefreshing];
    }];
}
#pragma mark - 点击的哪一行的要分享
-(void)clickButShare :(UIButton *)but
{
    self.shareAndDownloadNumberRow = but.tag;
    
    if(self.numberRow == but.tag){
        self.viewSharePopover.hidden = YES;
        self.viewLow.hidden = YES;
        self.butCancel.hidden = YES;
        [LCFloatingWindow sharedInstance].hidden = NO  ;
        self.numberRow = -1;
    }else{
        self.viewSharePopover.hidden = NO;
        self.viewLow.hidden = NO;
        self.butCancel.hidden = NO;
        [LCFloatingWindow sharedInstance].hidden = YES;
        self.numberRow = but.tag;
    }
}
-(UITableView *)tableviewPoetry
{
    if (_tableviewPoetry == nil) {
        _tableviewPoetry = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        //x +10
        _tableviewPoetry.delegate = self;
        _tableviewPoetry.dataSource = self;
        //添加以下代码关闭估算行高
        _tableviewPoetry.estimatedRowHeight = 0;
        _tableviewPoetry.estimatedSectionHeaderHeight = 0;
        _tableviewPoetry.estimatedSectionFooterHeight = 0;
        [_tableviewPoetry setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableviewPoetry];
    }
    return _tableviewPoetry;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.mutArrayData.count;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LSZRandombroadcastHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZRandombroadcastHeaderCell"];
    if (!cell) {
        cell = [[LSZRandombroadcastHeaderCell alloc]initWithReuseIdentifier:@"LSZRandombroadcastHeaderCell"];
    }
    UITapGestureRecognizer * singleTapimageView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickButRandom:)];
    [cell.imageViewPlay addGestureRecognizer:singleTapimageView];
    UITapGestureRecognizer * singleTaplabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickButRandom:)];
    [cell.label addGestureRecognizer:singleTaplabel];
    [cell.butPaly addTarget:self action:@selector(clickButRandom:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma mark - 点击随机播放
-(void)clickButRandom:(UIButton *)but
{
    LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
    _LCPlayer.songs = playlistTest.allSongs;
    _LCPlayer.listStatus = LCMusicListStatusAllCycle;
    [_LCPlayer playAtIndex:0 isSelectPlay:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZAudioFrequencyTableViewCell * cell = [[LSZAudioFrequencyTableViewCell alloc]initWithNumberAudioFrequency:0];
    LSZPoetryMg * poetry = nil;
    poetry = self.mutArrayData[indexPath.row];
    cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    cell.imageViewHook.image = [UIImage imageNamed:@"首页_诗歌_对号"];
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",poetry.title];
    cell.labelChapters.text = [NSString stringWithFormat:@"%@",poetry.album];
    cell.butShare.tag = indexPath.row;
    [cell.butShare addTarget:self action:@selector(clickButShare:) forControlEvents:UIControlEventTouchUpInside];
    if (_LCPlayer.selectedIndex == indexPath.row && [[LCHistoryDataManager loadOneSongsData]  isEqualToString:cell.labelTitle.text]&&[cell.labelChapters.text isEqualToString:_LCPlayer.playingSong.album]) {
        [cell clickOnThisSongToPlay:YES];
    }else{
        [cell clickOnThisSongToPlay:NO];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - 单元格的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZPoetryMg * poetry = nil;
    poetry = self.mutArrayData[indexPath.row];
    LSZLyricBooksVC * vc = [[LSZLyricBooksVC alloc]init];
    vc.URL = poetry.image;
    vc.titleStr = poetry.title;
    vc.lrcIndex = indexPath.row;
    vc.mutArrayData = self.mutArrayData;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 分享弹出来的view
-(UIView *)viewSharePopover
{
    if(_viewSharePopover == nil){
        [LSZSpecialPackaging initAlterView:self.view :^(UIView * viewBack,UIView * viewLow, UIButton * buttonFriends, UIButton * buttonFriendsCircle, UIButton * buttonDownload, UIButton * butCancel) {
            _viewSharePopover = viewBack;
            [self.view bringSubviewToFront:_viewSharePopover];
            [self.tableviewPoetry bringSubviewToFront:_viewSharePopover];

            self.viewLow = viewLow;
            [self.view bringSubviewToFront:self.viewLow];
            [self.tableviewPoetry bringSubviewToFront:self.viewLow];
            
            self.butCancel = butCancel;
            [self.view bringSubviewToFront:self.butCancel];
            [self.tableviewPoetry bringSubviewToFront:self.butCancel];
            
            [buttonFriends addTarget:self action:@selector(clickShareWeiXin) forControlEvents:UIControlEventTouchUpInside];
            [buttonFriendsCircle addTarget:self action:@selector(clickShareWeiXinFriendsCircle) forControlEvents:UIControlEventTouchUpInside];
            [buttonDownload addTarget:self action:@selector(clickDoenload) forControlEvents:UIControlEventTouchUpInside];
            [butCancel addTarget:self action:@selector(clickButCancel) forControlEvents:UIControlEventTouchUpInside];
        }];
    }
    return _viewSharePopover;
}
#pragma mark - 点击微信分享好友
-(void)clickShareWeiXin
{
    NSString * strUrl = [[NSString alloc]init];
    NSString * strTitle = [[NSString alloc]init];
    LSZPoetryMg * poetry = nil;
    poetry = self.mutArrayData[self.shareAndDownloadNumberRow];
    strUrl = poetry.fileUrl;
    strTitle = poetry.title;
    NSArray* imageArray = @[[UIImage imageNamed:@"placeholder"]];
    [LSZSpecialPackaging shareWXWithViewController:self imageArray:imageArray url:strUrl title:strTitle contentMessage:@"" type:0];
    [self clickButCancel];
}
#pragma mark - 点击微信分享朋友圈
-(void)clickShareWeiXinFriendsCircle
{
    NSString * strUrl = [[NSString alloc]init];
    NSString * strTitle = [[NSString alloc]init];
    LSZPoetryMg * poetry = nil;
    poetry = self.mutArrayData[self.shareAndDownloadNumberRow];
    strUrl = poetry.fileUrl;
    strTitle = poetry.title;
    NSArray* imageArray = @[[UIImage imageNamed:@"placeholder"]];
    [LSZSpecialPackaging shareWXWithViewController:self imageArray:imageArray url:strUrl title:strTitle contentMessage:@"" type:1];
    [self clickButCancel];
}
#pragma mark - 点击下载的按钮
-(void)clickDoenload
{
    LSZPoetryMg * poetry = nil;
    poetry = self.mutArrayData[self.shareAndDownloadNumberRow];
    NSDictionary *json = [poetry yy_modelToJSONObject];
    JKSongsMessStatus *song = [JKSongsMessStatus yy_modelWithJSON:json];
    [kNetworkTool downloadSongWithUrl:song];
    [self clickButCancel];
}
#pragma mark - 分享或者下载的view   下落
-(void)clickButCancel
{
    _viewSharePopover.hidden = YES;
    self.viewLow.hidden = YES;
    self.butCancel.hidden = YES;
    [LCFloatingWindow sharedInstance].hidden = NO  ;
    self.numberRow = -1;
}
@end
